<?php

namespace app\admin\model\finance;

use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use think\Db;
use think\Model;

class ClassRule extends Model
{

    // 表名
    protected $name = 'class_rule';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    // 追加属性
    protected $append = [

    ];

    public function getOriginData()
    {
        return $this->origin;
    }


    public function getGenderList()
    {
        return ['1' => __('Male'), '0' => __('Female')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['prevtime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['logintime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getJointimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['jointime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPrevtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setLogintimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setJointimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setBirthdayAttr($value)
    {
        return $value ? $value : null;
    }

    public function school()
    {
        return $this->belongsTo('app\admin\model\system\SchoolClass', 'school_class_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function room()
    {
        return $this->belongsTo('app\admin\model\system\ClassRoom', 'classroom_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function rule()
    {
        return $this->belongsTo('app\admin\model\system\TrusteeshipRule', 'project_class_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function myclass()
    {
        return $this->belongsTo('app\admin\model\MyClass', 'my_class_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function  getClassRule(){
        $where = [
            'a.id'=>['in',[11,12,13,14,15]],
          //  'a.status'=>'normal',
        ];
        $list =Db::name('auth_group')
            ->alias('a')
            ->where($where)
            ->order('id desc')
            ->select();
        $arr = [];
        foreach ($list as $w=>$v){
            $arr[$v['id']] = $v['name'];
            //    $arr[$w]['nickname'] = $v['nickname'];
        }
        return $arr;
    }

    public function  getRuleList(){
        $where = [
            'type'=>2
        ];
        $list =Db::name('rule_type')
            ->where($where)
            ->order('id desc')
            ->select();
        $arr = [];
        foreach ($list as $w=>$v){
            $v['tg_type_name'] = Db::name('trusteeship_type')->where(['id'=>['in',$v['tg_type']]])->column('type_name');
            $v['tg_type_name'] = implode(',',$v['tg_type_name']);
            $type_name = Db::name('trusteeship_type')->where(['id'=>['in',$v['yy_rule']]])->value('type_name');
            if($v['every_money']>0){
                $arr[$v['id']] = '应用类型('.$type_name.')'.'--(生效类型:'.$v['tg_type_name'].') 人数'.$v['people'].'人  '.$v['whole_money'].'/人/整月  '.$v['day_money'].'/人/单天'.'  超过人数'.$v['every_money'].'元/人';
            }else{
                $arr[$v['id']] ='应用类型('.$type_name.')'.'--(生效类型:'.$v['tg_type_name'].') 人数'.$v['people'].'人  '.$v['whole_money'].'/人/整月  '.$v['day_money'].'/人/单天';
            }


            //    $arr[$w]['nickname'] = $v['nickname'];
        }
        return $arr;
    }
    public function  getRuleNewList(){
        $where = [
            'type'=>3
        ];
        $list =Db::name('rule_type')
            ->where($where)
            ->order('id desc')
            ->select();
        $arr = [];
        foreach ($list as $w=>$v){
            $v['tg_type_name'] = Db::name('trusteeship_type')->where(['id'=>['in',$v['tg_type']]])->column('type_name');
            $v['tg_type_name'] = implode(',',$v['tg_type_name']);
            $type_name = Db::name('trusteeship_type')->where(['id'=>['in',$v['yy_rule']]])->value('type_name');
            if($v['every_money']>0){
                $arr[$v['id']] = '应用类型('.$type_name.')'.'--(生效类型:'.$v['tg_type_name'].') 人数'.$v['people'].'人  '.$v['whole_money'].'/人/整月  '.$v['day_money'].'/人/单天'.'  超过人数'.$v['every_money'].'元/人  '. $v['every_whole_money'].'/人/单天';
            }else{
                $arr[$v['id']] = '应用类型('.$type_name.')'.'--(生效类型:'.$v['tg_type_name'].') 人数'.$v['people'].'人  '.$v['whole_money'].'/人/整月  '.$v['day_money'].'/人/单天';
            }
            //    $arr[$w]['nickname'] = $v['nickname'];
        }
        return $arr;
    }
    public function  getRuleGList(){
        $where = [
            'type'=>1
        ];
        $list =Db::name('rule_type')
            ->where($where)
            ->order('id desc')
            ->select();
        $arr = [];
        foreach ($list as $w=>$v){
            $v['tg_type_name'] = Db::name('trusteeship_type')->where(['id'=>['in',$v['tg_type']]])->column('type_name');
            $type_name = Db::name('trusteeship_type')->where(['id'=>['in',$v['yy_rule']]])->value('type_name');
            $v['tg_type_name'] = implode(',',$v['tg_type_name']);
            if($v['every_money']>0){
                $arr[$v['id']] = '应用类型('.$type_name.')'.'--(生效类型:'.$v['tg_type_name'].') 人数'.$v['people'].'人  '.$v['whole_money'].'/人/整月  '.$v['day_money'].'/人/单天'.'  超过人数'.$v['every_money'].'元/人';
            }else{
                $arr[$v['id']] = '应用类型('.$type_name.')'.'--(生效类型:'.$v['tg_type_name'].') 人数'.$v['people'].'人  '.$v['whole_money'].'/人/整月  '.$v['day_money'].'/人/单天';
            }


            //    $arr[$w]['nickname'] = $v['nickname'];
        }
        return $arr;
    }
    public function  getTypeList(){
        return ['3'=>'全托','4'=>'晚托','5'=>'午托'];
    }
}
